package cn.fhkj.service.impl;

import cn.fhkj.bean.ResultBean;
import cn.fhkj.dao.*;
import cn.fhkj.entity.*;
import cn.fhkj.service.RenqiuZfService;
import cn.fhkj.vo.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @program: renqiu-publicity-web
 * @description: 任丘执法业务实现类
 * @author: seven
 * @create: 2020-08-06 10:04
 **/
@Service
@Transactional(rollbackFor = Exception.class)
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
public class RenqiuZfServiceImpl extends DefaultService implements RenqiuZfService {
    private static final Logger logger = LoggerFactory.getLogger(RenqiuZfServiceImpl.class);


    @Resource
    private RenQiuZfPublicityTypeLv1Dao renQiuZfPublicityTypeLv1Dao;
    @Resource
    private RenQiuZfPublicityTypeLv2Mapper renQiuZfPublicityTypeLv2Mapper;
    @Resource
    private RenQiuZfPublicityMapper renQiuZfPublicityMapper;
    @Resource
    private RenQiuZfPublicityContentMapper renQiuZfPublicityContentMapper;
    @Resource
    private RenQiuZfUnitMapper renQiuZfUnitMapper;
    @Resource
    private RenqiuZfPublicityReconsiderationMapper reconsiderationMapper;
    @Resource
    private RenqiuZfPublicityUnitTypeMapper renqiuZfPublicityUnitTypeMapper;

    /**
     * 查询1级列表
     * @return
     */
    @Override
    public ResultBean getRenQiuLv1() {
        ResultBean resultBean = new ResultBean();
        try{
            List<RenQiuZfPublicityTypeLv1Vo> voList = new ArrayList<>();
            RenQiuZfPublicityTypeLv1Vo vo ;
            List<RenQiuZfPublicityTypeLv1> lv1List =  renQiuZfPublicityTypeLv1Dao.getRenQiuLv1All();
            if (lv1List.size() > 0 &&!lv1List.isEmpty()){
                for (RenQiuZfPublicityTypeLv1 l1: lv1List) {
                vo = new RenQiuZfPublicityTypeLv1Vo();
                    vo.setId((l1.getId()).toString());
                    vo.setName(l1.getName());
                    voList.add(vo);
                }
            }
            resultBean.setData(lv1List);
            resultBean.setMsg("查询成功！");
            resultBean.setSuccess(ResultBean.SUCCESS);
        }catch (Exception e){
            logger.error("",e);
            resultBean.setMsg("查询失败！稍后重试");
            resultBean.setSuccess(ResultBean.FAIL);
        }
        return resultBean;
    }

    /**
     * 查询二级列表
     * @param index
     * @param pageSize
     * @param Lv1Id
     * @return
     */
    @Override
    public List<RenQiuZfPublicityTypeLv2Vo> getRenQiuLv2ByLv1(Integer index, Integer pageSize, Integer Lv1Id, Long unitId) {
        RenQiuZfPublicityTypeLv2Vo vo;
        List<RenQiuZfPublicityTypeLv2Vo> voList = new ArrayList<>();
        List<RenQiuZfPublicityTypeLv2> lv2List = new ArrayList<>();
        try {
            if (unitId == null){
                RenQiuZfPublicityTypeLv2Example lv2Example = new RenQiuZfPublicityTypeLv2Example();
                lv2Example.createCriteria().andLv1IdEqualTo(Long.valueOf(Lv1Id));
                lv2List = renQiuZfPublicityTypeLv2Mapper.selectByExample(lv2Example);
                if (lv2List.size() > 0 && !lv2List.isEmpty()) {
                    for (RenQiuZfPublicityTypeLv2 lv2  :lv2List) {
                        vo = new RenQiuZfPublicityTypeLv2Vo();
                        vo.setId((lv2.getId()).toString());
                        vo.setLv1Id(lv2.getLv1Id());
                        vo.setName(lv2.getName());
                        voList.add(vo);
                    }
                }
            }else {
                RenqiuZfPublicityUnitTypeExample unitType = new RenqiuZfPublicityUnitTypeExample();
                unitType.createCriteria().andUnitIdEqualTo(unitId);
                pagingReasonable(index, pageSize);
                List<RenqiuZfPublicityUnitType> renqiuZfPublicityUnitTypes = renqiuZfPublicityUnitTypeMapper.selectByExample(unitType);
                for (RenqiuZfPublicityUnitType r : renqiuZfPublicityUnitTypes) {
                    RenQiuZfPublicityTypeLv2Example lv2Example = new RenQiuZfPublicityTypeLv2Example();
                    lv2Example.createCriteria().andLv1IdEqualTo(Long.valueOf(Lv1Id)).andIdEqualTo(r.getLv2Id());
                    lv2List = renQiuZfPublicityTypeLv2Mapper.selectByExample(lv2Example);
                    if (lv2List.size() > 0 && !lv2List.isEmpty()) {
                        RenQiuZfPublicityTypeLv2 lv2 = lv2List.get(0);
                        vo = new RenQiuZfPublicityTypeLv2Vo();
                        vo.setId((lv2.getId()).toString());
                        vo.setLv1Id(lv2.getLv1Id());
                        vo.setName(lv2.getName());
                        vo.setUrl(r.getUrl());
                        voList.add(vo);
                    }
                }
            }
        } catch (Exception e) {
            logger.error("", e);
        }
        return voList;
    }

    /**
     * 查看详情信息
     * @param lv2Id
     * @return
     */
    @Override
    public List<RenQiuZfPublicityVo>  getPublicityByL2Id(Integer index, Integer pageSize, Long lv2Id, Long Lv1Id) throws ParseException {
        List<RenQiuZfPublicityVo> voList = new ArrayList<>();
        RenQiuZfPublicityVo vo ;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        RenQiuZfPublicityExample example = new RenQiuZfPublicityExample();
        RenQiuZfPublicityExample.Criteria c = example.createCriteria();
        if (lv2Id != null){
            c.andLv2TypeEqualTo(lv2Id);
        }
        if (Lv1Id != null){
            c.andLv1TypeEqualTo(Lv1Id);
        }
        example.setOrderByClause("create_time desc");
        pagingReasonable(index, pageSize);
        List<RenQiuZfPublicity> list = renQiuZfPublicityMapper.selectByExample(example);
        if (list.size()>0 && !list.isEmpty()){
            for (RenQiuZfPublicity r:list) {
                vo = new RenQiuZfPublicityVo();
                if (r.getCreateTime() != null){
                    Date format = sdf.parse(r.getCreateTime());
                    vo.setCreateTime(sdf.format(format));
                }
                vo.setId((r.getId()).toString());
                vo.setLv1Type((r.getLv1Type()).toString());
                vo.setLv2Type((r.getLv2Type()).toString());
                vo.setResType(r.getResType());
                vo.setResUrl(handleResToUrl(r.getResUrl()));
                vo.setSort(r.getSort());
                vo.setStatus(r.getStatus());
                vo.setTitle(r.getTitle());
                vo.setUnitId((r.getUnitId()).toString());
                voList.add(vo);
            }
        }
        return voList;
    }
    /**
     * 查看附文本信息
     */

    @Override
    public ResultBean getPublicityContentByPublicityId(Long publicityId) {
        ResultBean resultBean = new ResultBean();
        RenQiuZfPublicityContentVo vo =  new RenQiuZfPublicityContentVo();
        try {
            RenQiuZfPublicityContentExample example = new RenQiuZfPublicityContentExample();
            example.createCriteria().andFIdEqualTo(publicityId);
            List<RenQiuZfPublicityContent> list = renQiuZfPublicityContentMapper.selectByExampleWithBLOBs(example);
            if (!list.isEmpty() && list.size() > 0) {
                RenQiuZfPublicityContent content = list.get(0);
                vo.setId((content.getId()).toString());
                vo.setFId((content.getfId()).toString());
                String content1 = content.getContent();
                vo.setContent(htmlspecialchars(content1));
                resultBean.setData(vo);
                resultBean.setMsg("查询成功！");
                resultBean.setSuccess(ResultBean.SUCCESS);
            }
        } catch (Exception e) {
            logger.error("",e);
            resultBean.setMsg("查询失败！稍后重试");
            resultBean.setSuccess(ResultBean.FAIL);
        }
        return resultBean;
    }

    /**
     * 添加行政复议
     * @param vo
     * @return
     */
    @Override
    public ResultBean addReconsideration(ReconsiderationVo vo) {
        ResultBean resultBean = new ResultBean();
        RenqiuZfPublicityReconsideration reconsideration = new RenqiuZfPublicityReconsideration();
       try{
           SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
           reconsideration.setApplicantName(vo.getApplicantName());
            reconsideration.setApplicantSex(vo.getApplicantSex());
            reconsideration.setApplicantBirthday(vo.getApplicantBirthday());
            reconsideration.setApplicantCerName(vo.getApplicantCerName());
            reconsideration.setApplicantCerNum(vo.getApplicantCerNum());
            reconsideration.setApplicantWorkUnit(vo.getApplicantWorkUnit());
            reconsideration.setApplicantLocation(vo.getApplicantLocation());
            reconsideration.setApplicantPostalCode(vo.getApplicantPostalCode());
            reconsideration.setApplicantPhone(vo.getApplicantPhone());
            reconsideration.setApplicantEmail(vo.getApplicantEmail());
            reconsideration.setApplicantCompanyName(vo.getApplicantCompanyName());
            reconsideration.setApplicantCompanyNum(vo.getApplicantCompanyNum());
            reconsideration.setApplicantPerson(vo.getApplicantPerson());
            reconsideration.setApplicantLegalPerson(vo.getApplicantLegalPerson());
            reconsideration.setApplicantLegalPhone(vo.getApplicantLegalPhone());
            reconsideration.setApplicantLegalEmail(vo.getApplicantLegalEmail());
            reconsideration.setRespondentName(vo.getRespondentName());
            reconsideration.setRespondentLegalName(vo.getRespondentLegalName());
            reconsideration.setRespondentLocation(vo.getRespondentLocation());
            reconsideration.setRespondentPhone(vo.getRespondentPhone());
            reconsideration.setAgentName(vo.getAgentName());
            reconsideration.setAgentNation(vo.getAgentNation());
            reconsideration.setAgentSex(vo.getAgentSex());
            reconsideration.setAgentVocation(vo.getAgentVocation());
            reconsideration.setAgentBirthday(vo.getAgentBirthday());
            reconsideration.setAgentPostalCode(vo.getAgentPostalCode());
            reconsideration.setAgentLocation(vo.getAgentLocation());
            reconsideration.setAgentPhone(vo.getAgentPhone());
            reconsideration.setAgentFixedphone(vo.getAgentFixedphone());
            reconsideration.setAgentAddress(vo.getAgentAddress());
            reconsideration.setAgentCompanyName(vo.getAgentCompanyName());
            reconsideration.setAgentCompanyNum(vo.getAgentCompanyNum());
            reconsideration.setAgentLegalPerson(vo.getAgentLegalPerson());
            reconsideration.setAgentContacts(vo.getAgentContacts());
            reconsideration.setAgentContactsPhone(vo.getAgentContactsPhone());
            reconsideration.setAgentContactsEmail(vo.getAgentContactsEmail());
            reconsideration.setReconsiderationRequest(vo.getReconsiderationRequest());
            reconsideration.setFactsEvidence(vo.getFactsEvidence());
            reconsideration.setCreateTime(sdf.format(new Date()));
           int insert = reconsiderationMapper.insert(reconsideration);
           resultBean.setSuccess(ResultBean.SUCCESS);
           resultBean.setMsg("添加成功");
           resultBean.setData(reconsideration.getId());
       }catch (Exception e){
           logger.error("",e);
           resultBean.setSuccess(ResultBean.SUCCESS);
           resultBean.setMsg("添加失败");
        }
        return resultBean;
    }

    @Override
    public RenQiuZfPublicity getPublicityById(Long publicityId) {
        return renQiuZfPublicityMapper.selectByPrimaryKey(publicityId);
    }

    @Override
    public RenQiuZfUnit getUnitById(Long id) {
        return renQiuZfUnitMapper.selectByPrimaryKey(id);
    }

    /**
     * 置顶消息
     * @return
     */
    @Override
    public ResultBean getTopping() {
        ResultBean resultBean = new ResultBean();
        try {
            List<RenQiuZfPublicityVo> voList = new ArrayList<>();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            RenQiuZfPublicityVo vo ;
            RenQiuZfPublicityExample example = new RenQiuZfPublicityExample();
            example.createCriteria().andStatusEqualTo(1);
            example.setOrderByClause("create_time desc");
            List<RenQiuZfPublicity> list = renQiuZfPublicityMapper.selectByExample(example);
            if (list.size()>0 && !list.isEmpty()){
                for (RenQiuZfPublicity r:list) {
                    vo = new RenQiuZfPublicityVo();
                    RenQiuZfUnit renQiuZfUnit = renQiuZfUnitMapper.selectByPrimaryKey(r.getUnitId());
                    if (renQiuZfUnit != null ){
                        vo.setUnitName(renQiuZfUnit.getName());
                    }
                    if (r.getCreateTime() != null){
                        Date format = sdf.parse(r.getCreateTime());
                        vo.setCreateTime(sdf.format(format));
                    }
                    vo.setId((r.getId()).toString());
                    vo.setLv1Type((r.getLv1Type()).toString());
                    vo.setLv2Type((r.getLv2Type()).toString());
                    vo.setResType(r.getResType());
                    vo.setResUrl(handleResToUrl(r.getResUrl()));
                    vo.setSort(r.getSort());
                    vo.setStatus(r.getStatus());
                    vo.setTitle(r.getTitle());
                    vo.setUnitId((r.getUnitId()).toString());
                    voList.add(vo);
                }
            }
                resultBean.setData(voList);
            resultBean.setMsg("查询成功！");
            resultBean.setSuccess(ResultBean.SUCCESS);
        }catch (Exception e){
            logger.error("",e);
            resultBean.setMsg("查询失败！稍后重试");
            resultBean.setSuccess(ResultBean.FAIL);
        }
        return resultBean;
    }

    /**
     * 搜索框
     * @param name
     * @return
     */
    @Override
    public List<RenQiuZfPublicity> search(String name,Integer index,Integer pageSize,Long Lv1Id,Long Lv2Id,Long unitId) {

            RenQiuZfPublicityExample example = new RenQiuZfPublicityExample();
            RenQiuZfPublicityExample.Criteria criteria = example.createCriteria();
            if (name != null && !name.equals("")){
                criteria.andTitleLike("%"+name+"%");
            }if (unitId != null && !unitId.equals("")){
                criteria.andUnitIdEqualTo(unitId);
            }if (Lv1Id != null && !Lv1Id.equals("")){
                criteria.andLv1TypeEqualTo(Lv1Id);
            }if (Lv2Id != null && !Lv2Id.equals("")){
                criteria.andLv2TypeEqualTo(Lv2Id);
            }
            example.setOrderByClause("create_time desc");
            pagingReasonable(index, pageSize);
            List<RenQiuZfPublicity> list = renQiuZfPublicityMapper.selectByExample(example);

        return list;
    }

    @Override
    public List<RenQiuZfPublicityVo> dataProcessing(List<RenQiuZfPublicity> list) throws ParseException {
        List<RenQiuZfPublicityVo> voList = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        RenQiuZfPublicityVo vo ;
        if (list.size()>0 && !list.isEmpty()) {
            for (RenQiuZfPublicity r : list) {
                vo = new RenQiuZfPublicityVo();
                     RenQiuZfUnit renQiuZfUnit = renQiuZfUnitMapper.selectByPrimaryKey(r.getUnitId());
                    if (renQiuZfUnit != null ){
                        vo.setUnitName(renQiuZfUnit.getName());
                    }
                if (r.getCreateTime() != null){
                    Date format = sdf.parse(r.getCreateTime());
                    vo.setCreateTime(sdf.format(format));
                }
                vo.setId((r.getId()).toString());
                vo.setLv1Type((r.getLv1Type()).toString());
                vo.setLv2Type((r.getLv2Type()).toString());
                vo.setResType(r.getResType());
                vo.setResUrl((handleResToUrl(r.getResUrl())));
                vo.setSort(r.getSort());
                vo.setStatus(r.getStatus());
                vo.setTitle(r.getTitle());
                vo.setUnitId((r.getUnitId()).toString());
                voList.add(vo);
            }
        }else {
            return null;
        }
        return voList;
    }

    /**
     * 查询所有单位
     * @return
     */
    @Override
    public List<RenQiuZfUnit> getUnit() {
        RenQiuZfUnitExample example = new RenQiuZfUnitExample();
        example.createCriteria().andIdNotEqualTo(Long.valueOf(1));
        example.setOrderByClause("level");
        List<RenQiuZfUnit> lv1List = renQiuZfUnitMapper.selectByExample(example);
        return lv1List;
    }


}
